﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Kind Mark --

        /// <summary>
        /// This function shows list of Kind Mark.
        /// </summary>
        /// <returns></returns>
        public List<KindMark> GetListKindMark()
        {
            return DataContex.KindMarks.ToList();
        }

        #endregion

        #region Common

        private static bool KindMarkCommonValidator(KindMark kindMark, out string message)
        {
            if (string.IsNullOrWhiteSpace(kindMark.Name))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "tên Loại điểm");
                return false;
            }

            if (kindMark.Name.Length >= 50)
            {
                message = string.Format(ConstantMessages.FieldDataOverMaxLength, "tên Loại điểm", 50);
                return false;
            }
            message = string.Empty;
            return true;
        }

        #endregion       

        #region -- Edit Kind Mark --

      
        public bool EditKindMarkValidator(KindMark kindMark, out string message)
        {
            var id = kindMark.Id;

            if (!KindMarkCommonValidator(kindMark, out message)) return false;

            if (DataContex.KindMarks.Any(k => k.Name.ToLower() == kindMark.Name.ToLower() && k.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, kindMark.Name);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="kindMark"></param>
        /// <returns></returns>
        public bool EditKindMark(KindMark kindMark)
        {
            var id = kindMark.Id;
            var updateKindMark = DataContex.KindMarks.SingleOrDefault(f => f.Id == id);

            if (updateKindMark == null)
                return false;

            updateKindMark.Name = kindMark.Name;

            DataContex.SaveChanges();
            return true;
        }

        #endregion       
        
    }
}